import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

class Message{
    public String from;
    public String to;
    public  String message;
}

@WebServlet("/wall")
public class Wall extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    //  private String filePath = "g:/ee.txt";
//    Connection connection = null;
//    PreparedStatement preparedStatement = null;
//    ResultSet resultSet = null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf-8");
        List<Message> messageList = new ArrayList<>();
//        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
//        while (true){
//            String line = bufferedReader.readLine();
//            if (line == null){
//                break;
//            }
//            String[] cons = line.split("\t");
//            Message message = new Message();
//            message.from = cons[0];
//            message.to = cons[1];
//            message.message = cons[2];
//            messageList.add(message);
//        }
        //从数据库查找数据
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        if (connection == null){
            try {
                connection = UDBC.getCon();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        String sql = "select * from message";
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Message message = new Message();
                message.from = resultSet.getString("from");
                message.to = resultSet.getString("to");
                message.message = resultSet.getString("message");
                messageList.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                UDBC.close(connection,preparedStatement,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        objectMapper.writeValue(resp.getWriter(),messageList);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Message message = objectMapper.readValue(req.getInputStream(),Message.class);
//        FileWriter fileWriter = new FileWriter(filePath,true);
//        // 写入提交的数据
//        fileWriter.write(message.from + "\t" + message.to + "\t" + message.message + "\n");

//        try (FileWriter fileWriter = new FileWriter(filePath,true)){
//            fileWriter.write(message.from + "\t" + message.to + "\t" + message.message + "\n");
//        }catch (IOException e){
//            e.printStackTrace();
//        }
        //向数据库插入数据
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        if (connection == null){
            try {
                connection = UDBC.getCon();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            String sql = "insert into message values (?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,message.from);
            preparedStatement.setString(2,message.to);
            preparedStatement.setString(3,message.message);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                UDBC.close(connection,preparedStatement,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        resp.setContentType("application/json; charset=utf-8");
        resp.getWriter().write("{\"ok\": 1}");
    }
}
